package dshop.model.draw;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import com.jfinal.plugin.activerecord.Db;

import appbase.exception.BizzException;
import appbase.jfinal.ext.BaseModel;
import appbase.kit.encrypt.MD5Kit;
import appbase.utils.DateUtil;

public class DrawDetail extends BaseModel<DrawDetail> {

	private static final long serialVersionUID = -6967680902549117464L;

	public static final DrawDetail dao = new DrawDetail();

	// 状态 1 申请提现； 2 已确认；3 已转出；11 提现失败（被拒绝）
	public static final int STATUS_APPLY = 1;
	public static final int STATUS_CONFIRM = 2;
	public static final int STATUS_DRAW_SUCCESS = 3;
	public static final int STATUS_FAIL = 11;

	public static final Map<Integer, String> STATUS_STR_MAP = new HashMap<Integer, String>();

	static {
		STATUS_STR_MAP.put(STATUS_APPLY, "待确认");
		STATUS_STR_MAP.put(STATUS_CONFIRM, "待转账");
		STATUS_STR_MAP.put(STATUS_DRAW_SUCCESS, "提现成功");
		STATUS_STR_MAP.put(STATUS_FAIL, "提现失败");
	}
	public static void updateOperator(String operator,int id) {
		Db.update("update draw_detail set operator = ? , operator_time = now() where id = ?",operator,id);
	}
	/**
	 * 确认提现申请
	 * 
	 * @throws BizzException ("同一个提现请求不能重复确认") ("提现金额必须大于 0 元")
	 */
	public static void confirm(int drawId) throws BizzException {
		DrawDetail draw = dao.findByIdLoadColumns(drawId, " * ");
		if (draw == null || draw.getInt("status") != STATUS_APPLY) {
			throw new BizzException("同一个提现请求不能重复确认");
		}
		if (draw.getBigDecimal("money").compareTo(BigDecimal.ZERO) <= 0) {
			throw new BizzException("提现金额必须大于 0 元");
		}
		if (!checkSign(draw)) {
			throw new BizzException("非法的提现申请");
		}

		draw.set("status", STATUS_CONFIRM);
		String sign = generateDrawDetailSign(draw);

		new DrawDetail().set("id", drawId).set("status", draw.getInt("status")).set("sign", sign).update();
	}

	public static boolean checkSign(DrawDetail draw) {
		String sign = generateDrawDetailSign(draw.getInt("id"), draw.getInt("user_id"), draw.getBigDecimal("money"),
				draw.getStr("account_no"), draw.getStr("account_name"), draw.getInt("status"),
				draw.getDate("create_time"));
		return sign.equals(draw.getStr("sign"));
	}

	public static boolean checkSign(int id, int userId, BigDecimal money, String accountNo, String accountName,
			int status, Date createTime, String sign) {
		String newSign = generateDrawDetailSign(id, userId, money, accountNo, accountName, status, createTime);
		return newSign.equals(sign);
	}

	public static void update(int id, int status, String remark) throws BizzException {
		DrawDetail draw = dao.findByIdLoadColumns(id, " * ");
		if (!checkSign(draw)) {
			throw new BizzException("非法的提现记录");
		}

		draw.set("status", status).set("remark", remark).set("draw_time", new Date());
		String sign = generateDrawDetailSign(draw);

		draw.set("sign", sign).update();
	}

	public boolean canDraw() {
		int status = getInt("status");
		return status == STATUS_CONFIRM;
	}

	protected static String generateDrawDetailSign(DrawDetail draw) {
		return generateDrawDetailSign(draw.getInt("id"), draw.getInt("user_id"), draw.getBigDecimal("money"),
				draw.getStr("account_no"), draw.getStr("account_name"), draw.getInt("status"),
				draw.getDate("create_time"));
	}

	protected static String generateDrawDetailSign(int id, int userId, BigDecimal money, String accountNo,
			String accountName, int status, Date createTime) {
		StringBuilder builder = new StringBuilder();
		builder.append(accountNo).append("dU72").append(id).append("Hncs").append(money.setScale(2, RoundingMode.DOWN))
				.append(accountName).append("_").append(DateUtil.dateFormat("yyyyMMdd", createTime)).append("_U82")
				.append(userId).append("I6C_").append(status);
		String sign = MD5Kit.encrptMD5(builder.toString());
		return sign;
	}
}
